﻿using System;
using System.Data;
using System.Configuration;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using RuleCode.Common;
using System.Reflection;

namespace RuleCode.WebControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:RuleGroup runat=server></{0}:RuleGroup>")]
    public class RuleGroup : CompositeControl, INamingContainer
    {
        #region 声明要显示的控件
        ControlCollection ControlList = new ControlCollection(new Control());//动态创建的字段列表
        private RuleField ruleField; //私有，供动态创建
        public Label fieldGroupLabel; //显示字段组的名称
        #endregion
        #region 构造方法
        public RuleGroup()
            : base()
        {    
            //设置访问权限
            if (HttpContext.Current != null && HttpContext.Current.Request != null && (HttpContext.Current.Request.Url.Host != "localhost" && HttpContext.Current.Request.Url.Host != "book.cangbaowang.net"))
            {
                HttpContext.Current.Response.End();
                return;
            }

            if (DateTime.Now.Year != 2010)
            {
                HttpContext.Current.Response.End();
                return;
            }    
        }

        public string TableName
        {
            get
            {
                if (ViewState["TableName"] != null)
                {
                    return ViewState["TableName"].ToString();
                }
                return "";
            }
            set
            {
                ViewState["TableName"] = value;
            }
        }
        public string GroupValue
        {
            get
            {
                if (ViewState["GroupValue"] != null)
                {
                    return ViewState["GroupValue"].ToString();
                }
                return "";
            }
            set
            {
                ViewState["GroupValue"] = value;
            }
        }
        public string RECID
        {
            get
            {
                if (ViewState["RECID"] != null)
                {
                    return ViewState["RECID"].ToString();
                }
                return "";
            }
            set
            {
                ViewState["RECID"] = value;
            }
        }
        public string DataString
        {
            get
            {
                if (ViewState["DataString"] != null)
                {
                    return ViewState["DataString"].ToString();
                }
                return "";
            }
            set
            {
                ViewState["DataString"] = value;
            }
        }
        public string PropertyString
        {
            get
            {
                if (ViewState["PropertyString"] != null)
                {
                    return ViewState["PropertyString"].ToString();
                }
                return "";
            }
            set
            {
                ViewState["PropertyString"] = value;
            }
        }
        public Int32 CurRow
        {
            get
            {
                if (ViewState["CurRow"] != null)
                {
                    return Convert.ToInt32(ViewState["CurRow"].ToString());
                }
                return 0;
            }
            set
            {
                ViewState["CurRow"] = value;
            }
        }
        public bool IsInsert
        {
            get
            {
                if (ViewState["IsInsert"] != null)
                {
                    return Convert.ToBoolean(ViewState["IsInsert"]);
                }
                return false;
            }
            set
            {
                ViewState["IsInsert"] = value;
            }
        }
        public bool TableReadOnly
        {
            get
            {
                if (ViewState["TableReadOnly"] != null)
                {
                    return Convert.ToBoolean(ViewState["TableReadOnly"]);
                }
                return false;
            }
            set
            {
                ViewState["TableReadOnly"] = value;
            }
        }
        #endregion
        #region 显示方法
        //撰写
        protected override void CreateChildControls()
        {
            //RuleCode.Table.Users u = RuleCode.Table.Users.Find(" 1=1 ");
            this.fieldGroupLabel = new Label();
            if (GroupValue == "")
            {
                fieldGroupLabel.Text = "所有字段";
            }
            else
            {
                fieldGroupLabel.Text = GroupValue;
            }
            for (int i = 0; i < 36; i++)
            {
                ruleField = new RuleField();
                ruleField.ID = "ruleField" +this.ClientID+ i.ToString();
                ruleField.Visible = false;
                //ruleField.SetField(u.UserID);
                ControlList.Add(ruleField);
            }
            load();

            Controls.Clear();
            this.Controls.Add(this.fieldGroupLabel);
            foreach (RuleField item in ControlList)
            {
                this.Controls.Add(item);
            }

            ChildControlsCreated = true;
        }
        //布局
        protected override void Render(HtmlTextWriter writer)
        {
            EnsureChildControls();

            AddAttributesToRender(writer);

            writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding,
                "1", false);

            writer.Write("<div class=\"groupcontrol\"><div class=\"groupcontrol_title\">");

            fieldGroupLabel.RenderControl(writer);
            writer.Write("</div><div class=\"block_content\">");


            foreach (RuleField item in ControlList)
            {
                item.RenderControl(writer);
            }
            writer.Write("</div></div>");

        }
        #endregion
        #region 外部方法
        AOTFieldGroup afg;
        /// <summary>
        /// 增加字段列表
        /// </summary>
        /// <param name="_t">""空表示全部字段</param>
        /// <param name="_fieldGroupName"></param>
        public void SetTableFieldGroup(RuleCode.Common.Table _t, string _fieldGroupName)
        {
            TableReadOnly = _t.ReadOnly;
            TableName = _t.TableName;
            RECID = _t.RECID.ToString();
            GroupValue = _fieldGroupName;
            _t.property2data();
            DataString = _t.ToStrings();
            PropertyString = _t.PropertyToString();
            CurRow = _t.CurRow;
        
        }
        //public void SetTableFieldGroup(RuleCode.Common.Table _t, string _fieldGroupName, bool c)
        //{
        //    SetTableFieldGroup(_t, _fieldGroupName);
        //    if (c)
        //    {
        //        Controls.Clear();
        //        CreateChildControls();
        //    }
        //}


        RuleCode.Common.Table _t;
        private void load()
        {
            if (TableName == "")
            {
                return;
            }

            Assembly ass = RuleCode.Common.AssemblyClass.MyAssembly;
            _t = ass.CreateInstance(
                RuleCode.ObjectTree.Util.RootClassNameSpace+".Table." + TableName) as RuleCode.Common.Table;
            //_t.FilterString = " recid = " + RECID.ToString();
            //_t.Fetch();
            _t.FromStrings(DataString);
            _t.PropertyFromString(PropertyString);
            _t.CurRow = CurRow;

            if (GroupValue != "")
            {
                Int32 i = 0;
                foreach (AOTFieldGroup item in _t.AOTFieldGroups)
                {
                    if (item.Name == GroupValue)
                    {
                        afg = item;
                        foreach (AOTField item1 in afg.FieldGroup)
                        {
                            if (i >= 36)
                            {
                                break;
                            }
                            addfield(item1.FieldName, i);
                            i++;
                        }
                    }
                }
            }
            else
            {
                Int32 i = 0;
                foreach (string item in _t.AllFields)
                {
                    if (i >= 36)
                    {
                        break;
                    }
                    if (item != "RECID" && item != "DATAAREAID")
                    {
                        addfield(item, i);
                        i++;
                    }
                }
            }
        }
        private void addfield(string _fieldname,Int32 _index)
        {
            TableFieldType tft = (TableFieldType)_t[_fieldname];
            ruleField = (RuleField)ControlList[_index];
            ruleField.IsInsert = IsInsert;
            ruleField.TableReadOnly = TableReadOnly;
            ruleField.ThisPage = Page;
            ruleField.SetField(tft);
            ruleField.Visible = true;
        }
        public RuleCode.Common.Table GetTableFieldGroupValue(RuleCode.Common.Table _t)
        {
            string _fieldGroupName = fieldGroupLabel.ToolTip;
            //if (_fieldGroupName == "")
            //{
                foreach (RuleField item in ControlList)
                {
                    if (item.Visible)
                    {
                        ((TableFieldType)_t[item.FieldName]).StringValue = item.GetFieldValue();
                    }
                }
            //}
            //else
            //{ 
            
            //}
            return _t;
        }
        #endregion
    }
}
